package com.cec.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cec.dto.InfoDTO;
import com.cec.entity.Info;
import com.cec.exception.DataLoadingException;
import com.cec.mapper.admin.InfoMapper;
import com.cec.result.Result;
import com.cec.result.ResultCode;
import com.cec.service.user.UserInfoService;
import com.cec.vo.InfoVO;
import com.cec.vo.CategoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 资讯表 服务实现类
 * </p>
 *
 * @author lt
 * @since 2024-10-25
 */
@Slf4j
@Service("userInfoService")
public class UserInfoServiceImpl extends ServiceImpl<InfoMapper, Info> implements UserInfoService {

    @Override
    public Result pageQueryInfo(InfoDTO infoDTO) {
        QueryWrapper<InfoDTO> wrapper = new QueryWrapper<>();
        if (infoDTO.getInfoTitle()!=null && !"".equals(infoDTO.getInfoTitle()))
            wrapper.like("info_title", infoDTO.getInfoTitle());
        if (infoDTO.getInfoTypeId()!=null)
            wrapper.eq("info_type_id", infoDTO.getInfoTypeId());
        wrapper.eq("deleted", 0)
                .isNotNull("publish_time")
                .orderByDesc("publish_time");
        IPage<InfoVO> infoVOIPage = baseMapper.pageQueryInfo(new Page<>(infoDTO.getPage(),infoDTO.getPageSize()),wrapper);
        log.info("infoVOIPage.getRecords()-->{}",infoVOIPage.getRecords());
        log.info("infoVOIPage.getTotal()-->{}",infoVOIPage.getTotal());
        return Result.success(infoVOIPage.getRecords(),infoVOIPage.getTotal());
    }

    @Override
    public Result queryInfoType() {
        List<CategoryVO> categoryVOList = baseMapper.queryInfoType();
        return Result.success(categoryVOList);
    }

    @Override
    public Result queryInfoById(Integer id) {
        Info info = baseMapper.selectById(id);
        if (info == null)
            throw new DataLoadingException(ResultCode.DATA_LOADING_ERROR);
        return Result.success(info);
    }
}
